{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 进行数据扩增\n",
    "扩增思路：\n",
    "1、首先我们做归一化的像素值范围是可以自己选取的。（960x960，[1%,99%]做归一化；我们还可以选取2-3个像素值范围，手动指定，然后也做归一化）生成的归一化图像我们需要转为0-255（8位深）图像。\n",
    "2、在归一化图像的基础上，做左右翻转、上下翻转；旋转（小角度）；低剂量高斯滤波；\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow \n",
    "from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img\n",
    "import tifffile as tiff\n",
    "import os\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# keras 的图像扩增函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# data_gen_args = dict(\n",
    "#         rotation_range=20,\n",
    "#         width_shift_range=0,\n",
    "#         height_shift_range=0,\n",
    "#         shear_range=0,\n",
    "#         zoom_range=0,\n",
    "#         horizontal_flip=True,\n",
    "#         vertical_flip=True,\n",
    "#         fill_mode='reflect)\n",
    "# image_datagen = ImageDataGenerator(**data_gen_args)\n",
    "# mask_datagen  = ImageDataGenerator(**data_gen_args)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "datagen = ImageDataGenerator(\n",
    "        rotation_range=15,\n",
    "        width_shift_range=0,\n",
    "        height_shift_range=0,\n",
    "        shear_range=0,\n",
    "        zoom_range=0,\n",
    "        horizontal_flip=True,\n",
    "        vertical_flip=True,\n",
    "        fill_mode='reflect')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "if not os.path.exists('input/expansion_image'):\n",
    "    os.makedirs('input/expansion_image')\n",
    "if not os.path.exists('input/expansion_label'):\n",
    "    os.makedirs('input/expansion_label')\n",
    "# if not os.path.exists('input/expansion_red'):\n",
    "#     os.makedirs('input/expansion_red')\n",
    "# if not os.path.exists('input/expansion_gray'):\n",
    "#     os.makedirs('input/expansion_gray')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generation completed! Create \n"
     ]
    }
   ],
   "source": [
    "path = 'input/train/'\n",
    "# path = 'input/train_exposure/'\n",
    "pathDir =  os.listdir(path)\n",
    "for allDir in pathDir:\n",
    "    img = tiff.imread(path + allDir)\n",
    "    img = img[:,:,:3]\n",
    "    img = img.reshape((1,) + img.shape)\n",
    "    a = 0\n",
    "    ss = os.path.splitext(allDir.split(\".\")[0])[0]\n",
    "#     ss = ss + '_0'\n",
    "    for batch in datagen.flow(img, batch_size=32,save_to_dir='/home/CAD409/unet/jupyter/input/expansion_image', \n",
    "                              save_prefix= ss, save_format='tif',seed = 9):\n",
    "        if a > 4:\n",
    "            break\n",
    "        a += 1\n",
    "\n",
    "print('Generation completed! Create ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# pathDir =  os.listdir(path)\n",
    "# for allDir in pathDir:\n",
    "#     img = tiff.imread(path + allDir)\n",
    "#     img = img[:,:,3]\n",
    "#     img = img.reshape((1,) + img.shape + (1,))\n",
    "#     a = 0\n",
    "#     ss = os.path.splitext(allDir.split(\".\")[0])[0]\n",
    "# #     ss = ss + '_0'\n",
    "#     for batch in datagen.flow(img, batch_size=32,save_to_dir='/home/CAD409/unet/jupyter/input/expansion_red', \n",
    "#                               save_prefix= ss, save_format='tif',seed = 9):\n",
    "#         if a > 6:\n",
    "#             break\n",
    "#         a += 1\n",
    "\n",
    "# print('Generation completed! Create ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# pathDir =  os.listdir(path)\n",
    "# for allDir in pathDir:\n",
    "#     img = tiff.imread(path + allDir)\n",
    "#     img = img[:,:,4]\n",
    "#     img = img.reshape((1,) + img.shape + (1,))\n",
    "#     a = 0\n",
    "#     ss = os.path.splitext(allDir.split(\".\")[0])[0]\n",
    "# #     ss = ss + '_0'\n",
    "#     for batch in datagen.flow(img, batch_size=32,save_to_dir='/home/CAD409/unet/jupyter/input/expansion_gray', \n",
    "#                               save_prefix= ss, save_format='tif',seed = 12):\n",
    "#         if a > 10:\n",
    "#             break\n",
    "#         a += 1\n",
    "\n",
    "# print('Generation completed! Create ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generation completed! Create \n"
     ]
    }
   ],
   "source": [
    "path = 'input/label/'\n",
    "# path = 'input/label_exposure/'\n",
    "pathDir =  os.listdir(path)\n",
    "for allDir in pathDir:\n",
    "    img = cv2.imread(path + allDir)\n",
    "    img = img.reshape((1,) + img.shape)\n",
    "    a = 0\n",
    "    ss = os.path.splitext(allDir.split(\".\")[0])[0]\n",
    "#     ss = ss + '_0'\n",
    "    for batch in datagen.flow(img, batch_size=32,save_to_dir='/home/CAD409/unet/jupyter/input/expansion_label', \n",
    "                              save_prefix= ss, save_format='jpg',seed = 9):\n",
    "        if a > 4:\n",
    "            break\n",
    "        a += 1\n",
    "\n",
    "print('Generation completed! Create ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# import numpy as np\n",
    "# if not os.path.exists('input/expansion_final_image'):\n",
    "#     os.makedirs('input/expansion_final_image')\n",
    "    \n",
    "# path_img = 'input/expansion_image/'\n",
    "# path_red = 'input/expansion_red/'\n",
    "# # path_gray = 'input/expansion_gray/'\n",
    "# pathDir =  os.listdir(path_img)\n",
    "# for allDir in pathDir:\n",
    "#     img = tiff.imread(path_img + allDir)\n",
    "#     img_red = tiff.imread(path_red + allDir)\n",
    "#     img_red = img_red.reshape(img_red.shape + (1,))\n",
    "# #     img_gray = tiff.imread(path_gray + allDir)\n",
    "#     x = np.array(img)\n",
    "#     y = np.array(img_red)\n",
    "# #     y = np.concatenate((np.array(img_red),np.array(img_gray)),aixs = 2)\n",
    "#     z = np.concatenate((x,y),axis = 2)\n",
    "#     ss = 'input/expansion_final_image/' + allDir\n",
    "#     tiff.imsave(ss, z)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
